# -*- coding: utf-8 -*-

from .hw_defs import *


def __flatten(args):
    flat_list = []
    # Check arguments.
    if isinstance(args, list):
        for item in args:
            flat_list += __flatten(item)
        return flat_list
    if isinstance(args, tuple):
        return __flatten(list(args))
    if isinstance(args, int):
        return [args]
    if isinstance(args, range):
        return list(args)
    raise Exception('Unimplemented flatten types {}'.format(type(args)))


def __mtl_bitmap(*args) -> int:
    # Loop and create mask.
    mask = 0
    flat_args = __flatten(args)
    # Generate the pid info.
    for pid in flat_args:
        mask |= (1 << pid)
    # Give back the mask
    return mask


def mtl_pid_all():
    return __mtl_bitmap(*list(range(VARS.num_of_peer)))


def mtl_alu_all():
    return __mtl_bitmap(*list(range(VARS.num_of_alu)))


def mtl_pid(*args):
    return __mtl_bitmap(args)


def mtl_alu(*args):
    return __mtl_bitmap(args)


def mtl_pid_main_mem():
    return -1